package com.joint.base.dao.impl;

import com.fz.us.base.bean.BaseEnum;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.joint.base.dao.CommonConfigDao;
import com.joint.base.entity.*;
import com.joint.base.service.DutyService;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


/**
 * Dao实现类 - 
 * ============================================================================
 * 版权所有 2013 。
 * 
 * @author 
 * @version 0.1 2013-01-06
 * ============================================================================
 */

@Repository
public class CommonConfigDaoImpl extends BaseEntityDaoImpl<CommonConfig, String> implements CommonConfigDao {
    @Resource
    private DutyService dutyService;

    @Override
    public List<Role> findRoleById(String id) {
        Assert.notNull(id,"id is required");
        String hql = "select role from CommonConfig as cfg left join cfg.roleSet as role where cfg.id=:n and role.state=:s";
        List<Role> roleList = getSession().createQuery(hql).setParameter("n",id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        if(roleList.size()>0){
            return roleList;
        }else{
            return Lists.newArrayList();
        }
    }

    @Override
    public List<Department> findDepartmentById(String id) {
        Assert.notNull(id,"id is required");
//        String hql="select dept from CommonConfig as cfg left join cfg.departmentSet as dept where (cfg.id=:n and cfg.state=:s)";
        String hql="select cfg.departmentSet from CommonConfig as cfg where (cfg.id=:n and cfg.state=:s)";
        List<Department> deptList = getSession().createQuery(hql).setParameter("n",id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        if(deptList.size()>0){
            return deptList;
        }else{
            return Lists.newArrayList();
        }
    }

    @Override
    public List<Post> findPostById(String id) {
        Assert.notNull(id,"id is required");
        String hql="select post from CommonConfig as cfg left join cfg.postSet as post on cfg.id=:n and post.state=:s";
        List<Post> postList = getSession().createQuery(hql).setParameter("n",id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        if(postList.size()>0){
            return postList;
        }else{
            return Lists.newArrayList();
        }
    }

    @Override
    public List<Power> findPowerById(String id) {
        Assert.notNull(id,"id is required");
        String hql="select power from CommonConfig as cfg left join cfg.powerSet as power on cfg.id=:n and power.state=:s";
        List<Power> powerList = getSession().createQuery(hql).setParameter("n", id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        if(powerList.size()>0){
            return powerList;
        }else{
            return Lists.newArrayList();
        }
    }

    @Override
    public List<Duty> findDutyByCfgInDepart(String id) {
        Assert.notNull(id,"id is required");
        String hql = "from Duty as duty where duty.department in ( select dep from CommonConfig as cfg left join cfg.departmentSet as dep where cfg.id=:n) and duty.state=:s";
        List<Duty> dutyList = getSession().createQuery(hql).setParameter("n",id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        return dutyList;
    }

    @Override
    public List<Duty> findDutyByCfgInPost(String id) {
        Assert.notNull(id,"id is required");
        String hql = "from Duty as duty where duty.post in ( select post from CommonConfig as cfg left join cfg.postSet as post where cfg.id=:n) and duty.state=:s";
        List<Duty> dutyList = getSession().createQuery(hql).setParameter("n", id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        return dutyList;
    }

    @Override
    public List<Duty> findDutyByCfgInPower(String id) {
        Assert.notNull(id,"id is required");
        String hql = "from Duty as duty where duty.power in ( select power from CommonConfig as cfg left join cfg.powerSet as power where cfg.id=:n) and duty.state=:s";
        List<Duty> dutyList = getSession().createQuery(hql).setParameter("n", id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        return dutyList;
    }

    @Override
    public List<Duty> findDutyByCfgInRole(String id) {
        Assert.notNull(id, "id is required");
        String hql = "from Duty as duty where (duty.power,duty.department,duty.users) in " +
                "(select power,depart,users from CommonConfig as cfg " +
                "left join cfg.roleSet as role left join role.powerSet as power left join role.usersSet as users left join role.departSet as depart " +
                "where cfg.id=:n) and duty.state=:s";
        List<Duty> dutyList = getSession().createQuery(hql).setParameter("n", id).setParameter("s", BaseEnum.StateEnum.Enable).list();
        return dutyList;
    }

    @Override
    public List<Duty> findDutyByConfig(String id) {
        List<Duty> dutyListRole=findDutyByCfgInRole(id);
        List<Duty> dutyListDepart=findDutyByCfgInDepart(id);
        List<Duty> dutyListPost=findDutyByCfgInPost(id);
        List<Duty> dutyListPower=findDutyByCfgInPower(id);

        List<Duty> dutyList=Lists.newArrayList();
        Set<Duty> dutySet= Sets.newHashSet();

        dutySet.addAll(dutyListRole);
        dutySet.addAll(dutyListDepart);
        dutySet.addAll(dutyListPost);
        dutySet.addAll(dutyListPower);
        dutyList.addAll(dutySet);

        return dutyList;
    }

}
